package com.my.helper.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.my.helper.R;
import com.my.helper.bean.AiData;

import java.util.ArrayList;
import java.util.List;

public class AiAdapter extends RecyclerView.Adapter<AiAdapter.ViewHolder> {

    private static final int ITEM_AI = 0;
    private static final int ITEM_ME = 1;

    private List<AiData> aiDataList = new ArrayList<>();

    public void addAiData(AiData aiData, RecyclerView recyclerView) {
        aiDataList.add(aiData);
        notifyDataSetChanged();
        recyclerView.smoothScrollToPosition(aiDataList.size() - 1);
    }

    @Override
    public AiAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == ITEM_AI) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ai, parent, false);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_me, parent, false);
        }
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(AiAdapter.ViewHolder holder, int position) {
        try {
            holder.tvContent.setText(aiDataList.get(position).getReply());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (aiDataList.get(position).item_type == ITEM_AI) {
            return ITEM_AI;
        }
        return ITEM_ME;
    }

    @Override
    public int getItemCount() {
        return aiDataList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvContent;

        public ViewHolder(View itemView) {
            super(itemView);
            tvContent = itemView.findViewById(R.id.tv_content);
        }
    }
}
